package com.ermao.learnWebmagic.processor_2;

import com.ermao.learnWebmagic.config.BilibiliAPIEnum;
import com.ermao.learnWebmagic.config.Config;
import com.ermao.learnWebmagic.entity.Video;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Json;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

public class VideoPageHandler {

	public void handle(Page page, CopyOnWriteArrayList<Video> result) {
		String curUrl = page.getRequest().getUrl();
		Json json = page.getJson();

		if (curUrl.startsWith(BilibiliAPIEnum.SEARCH.getUrlPrefix())) {
			// 获取UP 主的 mid 的并且拼接出 UP 主主页 URL
			List<String> midList = json.jsonPath("$.data.result[*].mid").all();
			midList = midList.stream().distinct().limit(Config.UP_NUMS).collect(Collectors.toList());

			for (String mid : midList) {
				page.addTargetRequest(new Request().setUrl(BilibiliAPIEnum.getAPI(BilibiliAPIEnum.UP_SPACE, mid)));
			}

		} else if (curUrl.startsWith(BilibiliAPIEnum.UP_SPACE.getUrlPrefix())) {

			int size = json.jsonPath("$.data.list.vlist[*]").all().size();

			for (int i = 0; i < (Math.min(size, Config.UP_VIDEO_NUMS)); i++) {
				String bvid = json.jsonPath(("$.data.list.vlist[" + i + "].bvid")).toString();
				String title = json.jsonPath(("$.data.list.vlist[" + i + "].title")).toString();
				String created = json.jsonPath(("$.data.list.vlist[" + i + "].created")).toString();
				String mid = json.jsonPath(("$.data.list.vlist[" + i + "].mid")).toString();

				Video video = Video.builder().video_title(title).video_created(Long.parseLong(created)).video_up_mid(mid).build();

				page.addTargetRequest(new Request().setUrl(BilibiliAPIEnum.getAPI(BilibiliAPIEnum.VIDEO_STAT, bvid)).putExtra(Config.EXTRA, video));
				page.addTargetRequest(new Request().setUrl("http://api.bilibili.com/x/tag/archive/tags?bvid=" + bvid + "&jsonp=jsonp").putExtra(Config.EXTRA, video));

			}

		} else if (curUrl.startsWith(BilibiliAPIEnum.VIDEO_STAT.getUrlPrefix())) {
			Video video = page.getRequest().getExtra(Config.EXTRA);
			video.setVideo_danmaku(Integer.parseInt(json.jsonPath("$.data.stat.danmaku").toString()));
			video.setVideo_play(Integer.parseInt(json.jsonPath("$.data.stat.view").toString()));
			video.setVideo_like(Integer.parseInt(json.jsonPath("$.data.stat.like").toString()));
			video.setVideo_coin(Integer.parseInt(json.jsonPath("$.data.stat.coin").toString()));
			video.setVideo_fav(Integer.parseInt(json.jsonPath("$.data.stat.favorite").toString()));
			video.setVideo_reply(Integer.parseInt(json.jsonPath("$.data.stat.reply").toString()));
			video.setVideo_share(Integer.parseInt(json.jsonPath("$.data.stat.share").toString()));

			afterHandle(video, result);
		} else if (curUrl.startsWith(BilibiliAPIEnum.VIDEO_TAG.getUrlPrefix())) {
			Video video = page.getRequest().getExtra(Config.EXTRA);
			List<String> tags = json.jsonPath("$.data.[*].tag_name").all();
			video.setVideo_tags(tags);

			afterHandle(video, result);
		}

	}

	private void afterHandle(Video video, CopyOnWriteArrayList<Video> result) {
		if (video.getVideo_title() != null && video.getVideo_tags() != null && video.getVideo_play() != null) {
			result.add(video);
		}
	}
}
